package com.kedacom.freedomLabel.printer;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.gprinter.command.EscCommand;
import com.gprinter.command.GpUtils;
import com.gprinter.command.LabelCommand;
import com.gprinter.command.LabelCommand.DIRECTION;
import com.gprinter.command.LabelCommand.EEC;
import com.gprinter.command.LabelCommand.FONTMUL;
import com.gprinter.command.LabelCommand.FONTTYPE;
import com.gprinter.command.LabelCommand.MIRROR;
import com.gprinter.command.LabelCommand.ROTATION;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.lzscale.scalelib.misclib.GPrinter;
import com.lzscale.scalelib.misclib.PrintStatusCallback;
import com.lzscale.scalelib.misclib.PrintStatusResult;

import java.util.Vector;

public class GpScalePrinter extends ScalePrinter<Business> {

    private static GpScalePrinter mGpScalePrinter = null;
    private GPrinter mPrinter;

    private GpScalePrinter() {
        this.mPrinter = new GPrinter();
    }

    public static GpScalePrinter getInstance() {
        if (mGpScalePrinter == null) {
            synchronized (GpScalePrinter.class) {
                if (mGpScalePrinter == null) {
                    try {
                        mGpScalePrinter = new GpScalePrinter();
                    } catch (Exception e) {
                        LLog.error("GpScalePrinter", e.getMessage());
                    }
                }
            }
        }
        return mGpScalePrinter;
    }

    @Override
    public void print(@NonNull final Business info, @Nullable final PrintStatusCallback callBack) {
        printThread.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    sendLabel(1, info);
                    if (callBack != null)
                        callBack.run(new PrintStatusResult(0, "success"));
                } catch (Exception e) {
                    if (callBack != null)
                        callBack.run(new PrintStatusResult(1, "failed:" + e.getMessage()));
                }
            }
        });
    }

    private void sendLabel(int count, Business bean) {
        String name = "name";
        String batch = "batch";
        String purchaseTime = "purchaseTime";
        String farmerName = "farmerName";
        String house = "house";

        LabelCommand tsc = new LabelCommand();
        tsc.addSize(60, 40); // 设置标签尺寸，按照实际尺寸设置
        tsc.addGap(4); // 设置标签间隙，按照实际尺寸设置，如果为无间隙纸则设置为0
        tsc.addDirection(DIRECTION.BACKWARD, MIRROR.NORMAL);// 设置打印方向
        tsc.addReference(0, 0);// 设置原点坐标
        tsc.addTear(EscCommand.ENABLE.ON); // 撕纸模式开启
        tsc.addCls();// 清除打印缓冲区
        tsc.addText(35, 10, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, FONTMUL.MUL_1, FONTMUL.MUL_1, batch);
        tsc.addQRCode(35, 40, EEC.LEVEL_L, 6, ROTATION.ROTATION_0, "http://www.baidu.com" + "?SN=" + batch);
        tsc.addText(245, 40, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, FONTMUL.MUL_2, FONTMUL.MUL_2, name);
        tsc.addText(245, 90, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, FONTMUL.MUL_1, FONTMUL.MUL_1, farmerName);
        int baseYS = 120, time = 0;
        String[] split = house.split(";");
        for (String gh : split) {
            int yl = baseYS + time * 30;
            tsc.addText(245, yl, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, FONTMUL.MUL_1, FONTMUL.MUL_1, gh);
            time++;
        }
        tsc.addText(35, 240, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, FONTMUL.MUL_1, FONTMUL.MUL_1, purchaseTime);
        tsc.addPrint(1, count); // 打印标签
        tsc.addSound(1, 100); // 打印标签后 蜂鸣器响
        Vector<Byte> datas = tsc.getCommand(); // 发送数据
        byte[] bytes = GpUtils.ByteTo_byte(datas);
        mPrinter.print(bytes);
    }

    public void print(final int count, @NonNull final Business info, @Nullable final PrintStatusCallback callBack) {
        printThread.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    sendLabel(count, info);
                    if (callBack != null)
                        callBack.run(new PrintStatusResult(0, "success"));
                } catch (Exception e) {
                    if (callBack != null)
                        callBack.run(new PrintStatusResult(1, "failed:" + e.getMessage()));
                }
            }
        });
    }
}
